import request from '@/utils/request'
import { AxiosPromise } from 'axios'
import { PmTaskVO, PmTaskQuery, PmTaskForm, PmTaskDependencyVO, PmTaskDependencyForm } from './types'

/**
 * 查询任务管理列表
 * @param query
 * @returns {*}
 */
export const listTask = (query?: PmTaskQuery): AxiosPromise<PmTaskVO[]> => {
  return request({
    url: '/pm/task/list',
    method: 'get',
    params: query
  })
}

/**
 * 查询任务管理详细
 * @param taskId
 */
export const getTask = (taskId: string | number): AxiosPromise<PmTaskVO> => {
  return request({
    url: '/pm/task/' + taskId,
    method: 'get'
  })
}

/**
 * 新增任务管理
 * @param data
 */
export const addTask = (data: PmTaskForm) => {
  return request({
    url: '/pm/task',
    method: 'post',
    data: data
  })
}

/**
 * 修改任务管理
 * @param data
 */
export const updateTask = (data: PmTaskForm) => {
  return request({
    url: '/pm/task',
    method: 'put',
    data: data
  })
}

/**
 * 删除任务管理
 * @param taskId
 */
export const delTask = (taskId: string | number | Array<string | number>) => {
  return request({
    url: '/pm/task/' + taskId,
    method: 'delete'
  })
}

/**
 * 获取任务树
 * @param projectId
 */
export const getTaskTree = (projectId: string | number): AxiosPromise<PmTaskVO[]> => {
  return request({
    url: '/pm/task/tree/' + projectId,
    method: 'get'
  })
}

/**
 * 更新任务状态
 * @param taskId
 * @param status
 */
export const updateTaskStatus = (taskId: string | number, status: string) => {
  return request({
    url: '/pm/task/' + taskId + '/status',
    method: 'put',
    params: { status }
  })
}

/**
 * 分配任务
 * @param taskId
 * @param assigneeId
 */
export const assignTask = (taskId: string | number, assigneeId: string | number) => {
  return request({
    url: '/pm/task/' + taskId + '/assign',
    method: 'put',
    params: { assigneeId }
  })
}

/**
 * 获取我的任务
 * @param status
 */
export const getMyTasks = (status?: string): AxiosPromise<PmTaskVO[]> => {
  return request({
    url: '/pm/task/my-tasks',
    method: 'get',
    params: { status }
  })
}

/**
 * 获取任务依赖关系
 * @param taskId
 */
export const getTaskDependencies = (taskId: string | number): AxiosPromise<PmTaskDependencyVO[]> => {
  return request({
    url: '/pm/task/' + taskId + '/dependencies',
    method: 'get'
  })
}

/**
 * 添加任务依赖
 * @param data
 */
export const addTaskDependency = (data: PmTaskDependencyForm) => {
  return request({
    url: '/pm/task/dependency',
    method: 'post',
    data: data
  })
}

/**
 * 删除任务依赖
 * @param dependencyId
 */
export const removeTaskDependency = (dependencyId: string | number) => {
  return request({
    url: '/pm/task/dependency/' + dependencyId,
    method: 'delete'
  })
}
